﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace Macds.Core.XML.Excel
{
    public class ExcelRowsCollection : ICollection<ExcelRow>
    {
        protected ArrayList _innerArray;

        public ExcelRowsCollection()
        {
            this._innerArray = new ArrayList();
        }

        public virtual ExcelRow this[int index]
        {
            get
            {
                return (ExcelRow)this._innerArray[index];
            }
            set
            {
                this._innerArray[index] = value;
            }
        }

        #region ICollection<ExcelRow> Members

        public void Add(ExcelRow item)
        {
            this._innerArray.Add(item);
        }

        public void AddRange(object[] objects)
        {
            ExcelRow row = new ExcelRow(objects);
            this._innerArray.Add(row);
        }

        public void Clear()
        {
            this._innerArray.Clear();
        }

        public bool Contains(ExcelRow item)
        {
            foreach (ExcelRow c in _innerArray)
            {
                if (c.UniqueId == item.UniqueId)
                {
                    return true;
                }
            }

            return false;
        }

        public void CopyTo(ExcelRow[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get
            {
                return this._innerArray.Count;
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(ExcelRow item)
        {
            bool result = false;

            for (int i = 0; i < _innerArray.Count; i++)
            {
                ExcelRow obj = (ExcelRow)_innerArray[i];

                if (obj.UniqueId == item.UniqueId)
                {
                    _innerArray.RemoveAt(i);
                    result = true;
                    break;
                }
            }

            return result;
        }

        #endregion

        #region IEnumerable<ExcelRow> Members

        public IEnumerator<ExcelRow> GetEnumerator()
        {
            return new ExcelRowsEnumerator(this);
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new ExcelRowsEnumerator(this);
        }

        #endregion
    }

    public class ExcelRowsEnumerator : IEnumerator<ExcelRow>
    {
        protected ExcelRowsCollection _collection;
        protected int index;
        protected ExcelRow _current;

        public ExcelRowsEnumerator() { }

        public ExcelRowsEnumerator(ExcelRowsCollection collection)
        {
            _collection = collection;
            index = -1;
            _current = default(ExcelRow);
        }

        #region IEnumerator<ExcelCell> Members

        public ExcelRow Current
        {
            get
            {
                return _current;
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            _collection = null;
            _current = default(ExcelRow);
            index = -1;
        }

        #endregion

        #region IEnumerator Members

        object IEnumerator.Current
        {
            get
            {
                return _current;
            }
        }

        public bool MoveNext()
        {
            if (++index >= _collection.Count)
            {
                return false;
            }
            else
            {
                _current = _collection[index];
            }

            return true;
        }

        public void Reset()
        {
            _current = default(ExcelRow);
            index = -1;
        }

        #endregion
    }
}
